//
//  MyNSTimerTool.m
//  AiKline
//
//  Created by tianlong on 16/7/10.
//  Copyright © 2016年 YJNetwork. All rights reserved.
//

#import "MyNSTimerTool.h"

#define TimerInterval 30

@interface MyNSTimerTool ()

@property (nonatomic, strong) NSTimer *timer;


@end


@implementation MyNSTimerTool


- (void)startNSTimerWithActionTimeInterval:(float)actionTimeInterval DelayTimeInterval:(float)delayTimeInterval repeats:(BOOL)repeats wantToDo:(void(^)())toDo{
    
    if (self.timer == nil) {
        
        if (delayTimeInterval <= 0) {
            //不需要延迟直接执行
            self.timer=[NSTimer scheduledTimerWithTimeInterval:actionTimeInterval target:self selector:@selector(toDoBlock) userInfo:nil repeats:repeats];
            [self.timer setFireDate:[NSDate distantPast]];
            
        }else{
            //需要延迟执行
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTimeInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 定时器创建与暂停
                self.timer=[NSTimer scheduledTimerWithTimeInterval:actionTimeInterval target:self selector:@selector(toDoBlock) userInfo:nil repeats:YES];
                [self.timer setFireDate:[NSDate distantPast]];
            });
        }
        
    }else{
        //如果定时器存在则继续
        if (delayTimeInterval <= 0) {
            //不需要延迟直接执行
            [self.timer setFireDate:[NSDate distantPast]];
        }else{
            //需要延迟执行
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTimeInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.timer setFireDate:[NSDate distantPast]];
            });
        }
    }
    
    if (toDo) {
        self.timeToDoBlock = toDo;
    }
}


- (void)startNSTimerWithActionTimeInterval:(float)actionTimeInterval repeats:(BOOL)repeats wantToDo:(void(^)())toDo
{
    
    if (self.timer == nil) {
        //不需要延迟直接执行
        self.timer=[NSTimer scheduledTimerWithTimeInterval:actionTimeInterval target:self selector:@selector(toDoBlock) userInfo:nil repeats:repeats];
        [self.timer setFireDate:[NSDate distantPast]];
    }else{
        //不需要延迟直接执行
        [self.timer setFireDate:[NSDate distantPast]];
    }
    if (toDo) {
        self.timeToDoBlock = toDo;
    }
}



#pragma mark - 延迟开启,需要主动调用
- (void)startNSTimerWithDelayTimeInterval:(float)delayTimeInterval repeats:(BOOL)repeats wantToDo:(void(^)())toDo
{
    
    if (self.timer == nil) {
        //不需要延迟直接执行
        self.timer=[NSTimer timerWithTimeInterval:delayTimeInterval target:self selector:@selector(toDoBlock) userInfo:nil repeats:NO];
    }else{
        //不需要延迟直接执行
        [self.timer setFireDate:[NSDate distantPast]];
    }
    if (toDo) {
        self.timeToDoBlock = toDo;
    }
}

#pragma mark - 手动开启定时器
-(void)ManualTimerWithActionTimeInterval:(float)actionTimeInterval repeats:(BOOL)repeats wantToDo:(void(^)())toDo
{
    if (toDo) {
        self.timeToDoBlock = toDo;
    }
    if (self.timer == nil) {
        //不需要延迟直接执行
        self.timer=[NSTimer scheduledTimerWithTimeInterval:actionTimeInterval target:self selector:@selector(toDoBlock) userInfo:nil repeats:repeats];
        [self pause];
    }
}


/**
 *  定时器要做的事
 */
- (void)toDoBlock{
    if (self.timeToDoBlock) {
        self.timeToDoBlock();
    }
}

#pragma mark - 开始计时
- (void)start
{
    if (self.timer != nil) {
        [self.timer setFireDate:[NSDate distantPast]];
    }
}

#pragma mark - 暂停
-(void)pause
{
    [self.timer setFireDate:[NSDate distantFuture]];
}


#pragma mark - 停止并移除
-(void)stopAndRemoveNSTimer
{
    [self.timer invalidate];
    self.timer = nil;
    
}


@end
